/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mapper;

import beans.BookBean;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author Haomaru
 */
public class BookMapper extends DBMapper {
     public BookMapper() throws Exception {
        super();
    }

    public BookBean getBookInformation(String ISBN) throws Exception {
        BookBean accBean = new BookBean();

        try {
            StringBuffer sql = new StringBuffer();
            sql.append("SELECT * FROM Book WHERE ");
            sql.append(" ISBN = '" + ISBN + "'");

            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
            if ((rs != null) && rs.next()) {
                initBookBean(accBean, rs);
            }

            stmt.close();
        } catch (Exception e) {
            throw e;
        }

        return accBean;
    }

    public void insertNewBook(BookBean book) throws Exception {
        try {
            StringBuffer sql = new StringBuffer();
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            //...
            stmt.execute();
            stmt.close();
        } catch (Exception e) {
            throw e;
        }
    }

    public ArrayList<BookBean> listBook() throws Exception {
        ArrayList<BookBean> listofbooks = new ArrayList<BookBean>();
        BookBean bookBean = null;
        try {
            StringBuffer sql = new StringBuffer();
            sql.append("SELECT * FROM Book b, Book_Type t, Book_State s WHERE b.Type = t.ID and b.ISBN = s.ID");
            PreparedStatement stmt = getConnection().prepareStatement(sql.toString());
            ResultSet rs = stmt.executeQuery();
            while ((rs != null) && rs.next()) {
                bookBean = new BookBean();
                initBookBean(bookBean, rs);
                listofbooks.add(bookBean);
            }

            stmt.close();
        } catch (Exception e) {
            throw e;
        }

        return listofbooks;
    }

    public void initBookBean(BookBean bookBean, ResultSet rs) throws Exception {
        if ((rs != null) && (bookBean != null)) {
            bookBean.setISBN(rs.getString("isbn"));
            bookBean.setName(rs.getString("bookname"));
            bookBean.setType("type");
            bookBean.setDescribe("describe");
            bookBean.setPublisher(rs.getString("publisher"));
            bookBean.setYear(rs.getInt("year"));
            bookBean.setCost(rs.getDouble("cost"));
            bookBean.setQuantity(rs.getInt("quantity"));
            bookBean.setAuthor(rs.getString("author"));
            bookBean.setPicture(rs.getString("picture"));
            bookBean.setStatus(rs.getString("status"));
            bookBean.setState(rs.getString("state"));
        }
    }
}
